<?php
namespace Mapi\Controller;
use Mapi\Controller\BaseController;

class NoticeController extends BaseController {

	// public function notices($uid){
	// 	if($uid){
	// 		$userinfo=M('UserInfo')->find($uid);
	// 		if($userinfo){
	// 			$where='relation like "%'.$userinfo['userchannel']."-".$userinfo['userjobs'].',%"';
	// 			$notice=M("Notice")->where($where)->order('updatetime DESC')->select();
	// 			$arr['resultCode']="0";
	// 			$arr['message']=$notice;
	// 		}else{
	// 			$arr['resultCode']="1";
	// 			$arr['message']='用户不存在';
	// 		}
			
	// 	}else{
	// 		$arr['resultCode']="1";
	// 		$arr['message']='用户ID为空';
	// 	}
		
	// 	//dump($notice);
	// 	$this->ajaxReturn($arr);
	// }

    /**
     * 获取指定用户列表
     * @return [type] [description]
     */
    public function getOwnerNoticeList() {
    	// C("SHOW_PAGE_TRACE","True");
        $token = I('get.userToken');
        $pageSize = I('get.pageSize', 10);

        $userinfo = M('UserInfo')->find($uid);
        if($userinfo) {
        	C('VAR_PAGE', 'page');

        	$total =  M('NoticePush')->alias('np')
        				  ->join('__NOTICE__ AS n ON n.nid = np.nid')
        				  ->where(array('uid' => $this->uid))
        				  ->count();
	        $page = new \Think\Page($total, $pageSize);

        	$noticeList = M('NoticePush')->alias('np')
        				  ->join('__NOTICE__ AS n ON n.nid = np.nid')
        				  ->field('np.nid, n.title, n.content,np.createtime,np.isread')
        				  ->where(array('uid' => $this->uid))
        				  ->order('np.createtime desc')
        				  
        				  ->limit($page->firstRow .',' . $page->listRows)
        				  ->select();

        	if($noticeList) {
        		$list = array();
        		foreach($noticeList as $key => $val) {
        			$list[$key]['noticeId'] = $val['nid'];
        			$list[$key]['title'] = $val['title'];
        			//$list[$key]['createTime'] = date('Y-m-d H:i:s', $val['createtime']);
        			$list[$key]['createTime'] = $val['createtime'];
        			$list[$key]['content'] = $val['content'];
        			$list[$key]['isRead'] = $val['isread'];
        		}

 	   			$message['resultCode'] = 0;
 	   			$message['message'] = '';
				$message['noticeList'] = $list;
				$this->ajaxReturn($message);      		

        	} else {
	   			$message['resultCode'] = 0;
				$message['message'] = '暂无数据';
				$message['noticeList'] = array();
				$this->ajaxReturn($message);      		
        	}			  

        } else {
			$message['resultCode'] = "1";
			$message['message'] = '用户不存在';
			$this->ajaxReturn($message);  
		}
    }

    /**
     * 根据指定公告ID获取公告信息
     * @return [type] [description]
     */
    public function setNoticeStatusById($noticeId, $userToken, $flag = 0) {
    	if(!$noticeId) {
			$message['resultCode'] = 1;
			$message['message']='公告ID未知';
			return $message;    		
    	}

    	$dataGet['isread'] = 1;
    	$map['uid'] = $this->uid;
    	$map['nid'] = $noticeId;
    	$res = M('NoticePush')->where($map)->save($dataGet);
    	if($res !== false) {
  			$message['resultCode'] = 0;
			$message['message'] = '标记状态成功';  
 		
    	} else {
			$message['resultCode'] = 1;
			$message['message'] = '标记状态失败';    		
    	}

		if($flag == 0) {
			$this->ajaxReturn($message);
		} elseif($flag == 1) {
			return $message;	
		}
    }

    /**
     * 根据指定公告ID获得通告详情
     * @return [type] [description]
     */
    public function getNoitceById() {
    	$noticeId = I('get.noticeId', 0);
    	$map['nid'] = $noticeId;
    	$res = M('Notice')->where($map)->find();
    	if($res) {
    		$result = $this->setNoticeStatusById($noticeId, $token, 1);

  			$message['resultCode'] = 0;
  			$message['message'] = '';
			$message['noticeInfo'] = array(
				'noticeId' => $res['nid'],
				'title' => $res['title'],
				'content' => $res['content'],
				'createTime' => $res['createtime'],
				'isRead' => 1
			);    		
    	} else {
			$message['resultCode'] = -1;
			$message['message'] = '服务器端异常';
			    		
    	}

    	$this->ajaxReturn($message); 
    }
   /* public function pushNotice() {
    	$noticeId = I('get.noticeId', 0);
    	$map['nid'] = $noticeId;
    	$res = M('Notice')->where($map)->find();
    	if($res) {
    		$result = $this->setNoticeStatusById($noticeId, $token, 1);

  			$message['resultCode'] = 0;
  			$message['message'] = '';
			$message['noticeInfo'] = array(
				'noticeId' => $res['nid'],
				'title' => $res['title'],
				'content' => $res['content'],
				'createTime' => $res['createtime'],
				'isRead' => 1
			);    		
    	} else {
			$message['resultCode'] = -1;
			$message['message'] = '服务器端异常';
			    		
    	}

    	$this->ajaxReturn($message); 
    }*/

   	public function delNotice() {
   		apigetpost();
   		$noticeId = I('post.noticeId');
    	if(!$noticeId) {
			$message['resultCode'] = 1;
			$message['message']='公告ID未知';
			return $message;    		
    	}

   		$res = M('NoticePush')->where(array('nid' => $noticeId, 'uid' => $this->uid))->delete();
   		if($res) {
  			$message['resultCode'] = 0;
			$message['message'] = ''; 			
   		} else {
 			$message['resultCode'] = 1;
			$message['message'] = '删除失败'; 			
   		}

   		$this->ajaxReturn($message);
   	}

	/**
	 * 绑定推送标识
	 * @return [type] [description]
	 */
	public function bindChannelId() {
		apigetpost();
		$channelid = I('post.channelId');
		if($channelid) {
			$res = M('UserLoginInfo')->where(array('uid' => $this->uid))->save(array(
				'channelid' => $channelid,
			));		
			if($res) {
				$message['status'] = 0;
				$message['resultCode'] = '绑定成功';

			} else {
				$message['status'] = 1;
				$message['resultCode'] = '绑定失败1';				
			}

		} else {
			$message['status'] = 1;
			$message['resultCode'] = '绑定失败2';
		}

		$this->ajaxReturn($message);

	}
}